package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopu.blb_erp.entity.Order2;
import com.hopu.blb_erp.entity.Order2ToBeProduced;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.service.IOrder2Service;
import com.hopu.blb_erp.service.Order2ToBeProducedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Api
@Controller
@RestController
public class Order2ToBeProducedController {
    @Autowired
    private Order2ToBeProducedService order2ToBeProducedService;
    @Autowired
    private IOrder2Service order2Service;

    @ApiOperation("查询功能的分页")
    @GetMapping("/Order2ToBeProduced")
    public ResponseEntity<PageEntity<Order2ToBeProduced>> findProductHandoverPage(@RequestParam("current") Integer current, @RequestParam("PAGE_SIZE") Integer PAGE_SIZE
            , @RequestParam("orderId") String id, @RequestParam("status") String status ){
        int orderId = 0;
        int productStatus = 0;
        if(!"".equals(id)){ orderId = Integer.parseInt(id); }
        if("".equals(status)){
            productStatus=3;
        }else {
            productStatus=Integer.parseInt(status);
        }
        return ResponseEntity.ok(order2ToBeProducedService.findAllPage(current,PAGE_SIZE,orderId,productStatus)) ;
    }

    @ApiOperation("加入到激光码打印")
    @PutMapping("/Order2ToBeProduced/{id}")
    public ResponseEntity<String> addToLaser(@PathVariable("id") Integer id){
        //更新订单的商品状态为待入库
        order2Service.update(null, Wrappers.<Order2>lambdaUpdate().set(Order2::getProductStatus,3).eq(Order2::getId,id));
        //更新订单的商品的生产时间为当前时间
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        order2Service.update(null, Wrappers.<Order2>lambdaUpdate().set(Order2::getProductTime,df.format(date)).eq(Order2::getId,id));
        return ResponseEntity.ok("ok");
    }


}
